package com.hzit.service.impl;

import com.alibaba.fastjson.JSON;
import com.hzit.entity.LoginUser;
import com.hzit.service.TokenService;
import com.hzit.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 作者：李嘉浩
 * 时间：2024-08-31-9:41
 * 功能：
 */

@Service
public class TokenServiceImpl implements TokenService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Map<String, Object> getToken(LoginUser loginUser) {
        Map<String,Object> map=new HashMap<>();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        map.put("uuid",uuid);
        map.put("username",loginUser.getUser().getUsername());
        map.put("userId",loginUser.getUserId());
        String token = JwtUtils.createToken(map);

        stringRedisTemplate.opsForValue().set("access_token"+uuid, JSON.toJSONString(loginUser),30, TimeUnit.MINUTES);
        Map<String,Object> hashMap=new HashMap<>();
        hashMap.put("token",token);

        return hashMap;
    }
}
